package com.itheima.job;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * @Author : sunjun
 * @email : sunjun0207@163.com
 * @create 2019/11/10 10:13
 */
//自定义Job，实现定时清理垃圾图片

public class ClearImgJob {
    //注入jedisPool
    @Autowired
    private JedisPool jedisPool;
    //定义定时清除图片的方法clearImg
    public void clearImg(){
        //1.根据Redis中保存的两个set集合进行差值计算,
        //获得垃圾图片名称集合
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        //打印输出日志
        System.out.println("垃圾图片集合set:" + set);
        int i = 1;
        for (String picName : set) {
            System.out.println("第" + i +"张垃圾图片:" + picName);
            i++;
            //删除七牛云服务器上的图片
            QiniuUtils.deleteFileFromQiniu(picName);
            //从Redis中删除图片名称
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName);
        }

    }

}
